package com.bxd.baodan.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.LoginActivity;
import com.bxd.baodan.activity.MainActivity;
import com.bxd.baodan.activity.SettingActivity;
import com.bxd.baodan.activity.ShellActivity;
import com.bxd.baodan.activity.UserSettingActivity;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.app.SharedPreferanceKey;
import com.bxd.baodan.util.ImageUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.SpUtil;
import com.bxd.baodan.util.Utils;
import com.umeng.analytics.MobclickAgent;

public class MainSettingFragment_ extends BaseFragment implements OnClickListener {


    private Context context;
    private View view;
    private TextView login;



    private View loginLayout;

    private ImageView avatar;

    private LinearLayout feedback;
    private LinearLayout setting;




    private BroadcastReceiver receiveBroadCast;
    private TextView shell;
    private LinearLayout shell_ly;

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(this.getActivity(), R.layout.fragment_setting_new, null);
        }
        ViewGroup viewGroup = (ViewGroup) view.getParent();

        if (viewGroup != null) {
            viewGroup.removeAllViewsInLayout();
        }

        context = getActivity();

        return view;

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        initBroadCastReceiver();
//        top_ly = view.findViewById(R.id.top_ly);
//
//        top_ly.setPadding(top_ly.getPaddingLeft(), top_ly.getPaddingTop() + getStatusBarHeight(),
//                top_ly.getPaddingRight(), top_ly.getPaddingBottom());
        avatar = (ImageView) view.findViewById(R.id.avator);

        avatar.setFocusable(true);
        avatar.setFocusableInTouchMode(true);
        avatar.requestFocus();
        login = (TextView) view.findViewById(R.id.denglu);



        loginLayout =  view.findViewById(R.id.login_layout);
        avatar = (ImageView) view.findViewById(R.id.avator);



        feedback = (LinearLayout) view.findViewById(R.id.feedback);
        setting = (LinearLayout) view.findViewById(R.id.setting);

        shell_ly = (LinearLayout) view.findViewById(R.id.shell_ly);
        shell = (TextView) view.findViewById(R.id.shell);






        loginLayout.setOnClickListener(this);




        feedback.setOnClickListener(this);
        setting.setOnClickListener(this);
        shell_ly.setOnClickListener(this);


        loadLoginInfo();
        updateShell();



    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            // 点击登录
            case R.id.login_layout:

                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(),
                            "v6_1_3_WD_grzl");
                    startActivity(new Intent(App.getContext(),
                            UserSettingActivity.class));

                }
                break;



            // 反馈
            case R.id.feedback:

                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_yjfk");
                    String uid = LoginInfo.getInstance(context).getUser()
                            .getU_id();
                    openActivity(WebViewPager.class, new String[]{"startUrl"},
                            new String[]{Constant.FEEDBACK_URL + "uid="
                                    + uid + "&data=" + Utils.deviceJson(context)
                                    + "&kind=0"});
                }
                break;
            // 系统设置
            case R.id.setting:
                openActivity(SettingActivity.class);
                break;

            case R.id.shell_ly:

                if (loginStatus()) {
                    openActivity(ShellActivity.class);
                }
                break;
        }
    }


    public void loadLoginInfo() {
        if (LoginInfo.getInstance(App.getContext()).getUser()
                .getU_nick() != null) {
            this.login.setText( LoginInfo
                    .getInstance(App.getContext()).getUser()
                    .getU_nick());


        } else {
            this.login.setText("点击登录");

        }
        ImageUtil.setAvatarToRounderCorner(context, SpUtil.getInstance()
                .getString(SharedPreferanceKey.TEMPICURL), avatar, true);

    }


    public void updateShell() {
        if (LoginInfo.getInstance().isLogin()) {
            NetUtil netUtil = new NetUtil(getActivity(), JsonApi.USER_GETINTEGRAL);
            netUtil.postRequest("", new NetUtil.DataListener() {
                @Override
                public void onComplete(String data) {
                    String text = JSONUtil.getData("integral", data);
                    shell.setText(text+"贝壳");
                }
            });
        } else {
            shell.setText("");
        }
    }


    @Override
    public void onDestroy() {
        unRegistReceiver();
        super.onDestroy();
    }

    private void unRegistReceiver() {
        App.getContext().unregisterReceiver(receiveBroadCast);
    }

    private void initBroadCastReceiver() {
        // TODO Auto-generated method stub
        receiveBroadCast = new ReceiveBroadCast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.ACTION_SUCCESS_UPLOAD_INFOR);
        filter.addAction(Constant.ACTION_SUCCESS_MODIFIED_INFOR);
        filter.addAction(Constant.ACTION_LOGOUT);

        filter.addAction(Constant.ACTION_SUCCESS_LOGIN);
        App.getContext().registerReceiver(receiveBroadCast,
                filter);
    }


    public boolean loginStatus() {

        boolean isLogin = (LoginInfo.getInstance(App.getContext())
                .getUser().getU_id() != null);

        if (!isLogin) {
            openActivity(LoginActivity.class, new String[]{"from"}, new String[]{"1"});
        }
        return isLogin;
    }

    class ReceiveBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (Constant.ACTION_SUCCESS_UPLOAD_INFOR.equals(arg1.getAction())) {
                loadLoginInfo();

            } else if (Constant.ACTION_SUCCESS_MODIFIED_INFOR.equals(arg1
                    .getAction())) {
                updateHeaderUI();
            } else if (Constant.ACTION_LOGOUT.equals(arg1.getAction())) {
                updateHeaderUI();
                loadLoginInfo();
            }
            if (Constant.ACTION_SUCCESS_LOGIN.equals(arg1.getAction())) {
                loadLoginInfo();

                if (getActivity() != null) {
                    ((MainActivity) getActivity()).initPush();
                }
            }


        }


        private void updateHeaderUI() {
            if (LoginInfo.getInstance(App.getContext()).getUser()
                    .getU_nick() != null) {
                login.setText("Hi," + LoginInfo.getInstance(App.getContext())
                        .getUser().getU_nick());
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            updateShell();
        }
    }
}
